package com.talktalk.content;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Queue;

import com.talktalk.entity.Message;

public class MessageQueue {

	private Map<String,Queue<Message>> msgQueue = new HashMap<String, Queue<Message>>();

	
	public Message poll(String uid) {
		Queue<Message> queue = msgQueue.get(uid);
		Message poll = queue.poll();
		msgQueue.put(uid, queue);
		return poll;
	}
	
	public List<Message> pollAll(String uid){
		if(!msgQueue.containsKey(uid)){
			return null;
		}
		Queue<Message> queue = msgQueue.get(uid);
		List<Message> msgs = new ArrayList<Message>();
		while(!queue.isEmpty()&&queue != null){
			msgs.add(queue.poll());
		}
		return msgs;
	}
	
	public void add(String uid,Message msg){
		Queue<Message> q;
		if(msgQueue.containsKey(uid))
			q = msgQueue.get(uid);
		else
			q = new LinkedList<Message>();
		q.add(msg);
		msgQueue.put(uid, q);
	}
}
